package com.example.classsify.service.serviceImpl;

import com.example.classsify.mapper.ClaifyMapper;
import com.example.classsify.pojo.Classsify;
import com.example.classsify.service.ClaifyService;

import com.example.commons.utils.MyPageHelper;
import com.example.commons.utils.OssUtil;
import com.example.commons.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.util.Date;
import java.util.List;

/**
 * @Author 小江
 * @Description //TODO 分类管理 sercviceImpl
 * @Date 8:45 2022/11/15
 **/
@Service
public class ClaifyServiceImpl implements ClaifyService {

    @Autowired
    ClaifyMapper classsifyMapper;
    @Autowired
    OssUtil ossUtil;
    /**
     * @Author 小江
     * @Description //TODO 分类全查
     * @Date 8:45 2022/11/15
     **/
    @Override
    public PageResult classificationName(String classificationName, PageResult pageResult) {
        MyPageHelper.startPage(pageResult);
        Example example = new Example(Classsify.class);
        System.out.println(classificationName);
        Example.Criteria criteria = example.createCriteria().andEqualTo("delFlag", 0);
        if(!StringUtil.isEmpty(classificationName)){
            criteria.andLike("classificationName", "%" + classificationName + "%");
        }
        List<Classsify> classsifies = classsifyMapper.selectByExample(example);
        return MyPageHelper.getPageResult(classsifies);
    }

    /**
     * @Author 小江
     * @Description //TODO 分类添加/修改
     * @Date 10:30 2022/11/16
     **/
    @Override
    public int classsifyinfo( Classsify classsify, MultipartFile file) {
        if(file != null){
            String s = ossUtil.uploadFile(file);
            classsify.setLogoUrl(s);
        }
        System.out.println("classsify::"+classsify );
        Date date = new Date();
        classsify.setUpdateTime(date);
        if (classsify.getId() != null && classsify.getId() > 0) {
            // 修改
            int i = classsifyMapper.updateByPrimaryKeySelective(classsify);
            return i;
        } else {
            // 添加
            classsify.setCreateTime(date);
            classsify.setDelFlag("0");
            int i = classsifyMapper.insertSelective(classsify);
            return i;
        }
    }

    /**
     * @Author 小江
     * @Description //TODO 品牌删除
     * @Date 14:52 2022/11/16
     **/
    @Override
    public int classsifyDel(Integer id) {
        Classsify classsify = new Classsify();
        classsify.setId(id);
        classsify.setDelFlag("1");
        int i = classsifyMapper.updateByPrimaryKeySelective(classsify);
        return i;
    }
}
